package sis.studentinfo;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-23 19:32
 * @notify 课程 基类
 * @version 1.0
 */

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

abstract public class Session implements Comparable<Session>, Iterable<Student> , Serializable {
    private Course course;
    //报名该课程的学生
    private transient List<Student> students = new ArrayList<>();
    //课程开始时间
    private Date startDate;
    //该课程学分
    private int numberOfCredits;


    //构造子
    protected Session(Course course, Date startDate) {
        this.course = course;
        this.startDate = startDate;
    }


    //课程排序
    public int compareTo(Session that) {
        //课程名排序
        int compare = this.getDepartment().compareTo(that.getDepartment());
        if (compare != 0) return compare;
        //课程编号排序
        return this.getNumber().compareTo(that.getNumber());
    }

    //设置课程学分
    public void setNumberOfCredits(int numberOfCredits) {
        this.numberOfCredits = numberOfCredits;
    }

    //获取学分
    public int getNumberOfCredits(){
        return numberOfCredits;
    }

    //设置课程名称
    public String getDepartment() {
        return course.getDepartment();
    }

    //设置课程编号
    public String getNumber() {
        return course.getNumber();
    }

    //获取该课程报名学生列表
    public int getNumberOfStudents() {
        return students.size();
    }

    //学生报名
    public void enroll(Student student) {
        //添加学分
        student.addCredits(numberOfCredits);
        students.add(student);
    }

    //获取指定学生
    public Student get(int index) {
        return students.get(index);
    }

    //获取开始时间
    public Date getStartDate() {
        return startDate;
    }

    //获取全部学生
    public List<Student> getAllStudents() {
        return students;
    }

    //课程总共几周
    abstract protected int getSessionLength();

    //课程对于的网址
    public URL url;

    //设置网址
    public void setUrl(String urlString) throws SessionException {
        try {
            this.url = new URL(urlString);
        } catch (MalformedURLException e) {
            log(e);
            throw new SessionException(e);
        }
    }

    //处理异常
    private void log(Exception e) {
        //  e.printStackTrace();
    }

    //获取网址
    public URL getUrl() {
        return url;
    }

    //获取结束时间
    public Date getEndDate() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(getStartDate());
        final int daysInWeek = 7;
        final int daysFromFridayToMonday = 3;
        int numberOfDays = getSessionLength() * daysInWeek - daysFromFridayToMonday;
        calendar.add(Calendar.DAY_OF_YEAR, numberOfDays);
        return calendar.getTime();
    }

    //找出该科目学分不合格的学生,并计算他们的平均分
    public double averageGpaForPartTimeStudents() {
        //总分
        double total = 0.0;
        //不合格人数
        int count = 0;
        for (Student student : students) {
            //如果合格跳过
            if (student.isFullTime())
                continue;
            //不合格 不合格人数+1
            count++;
            total += student.getGpa();
        }
        if (count == 0) return 0.0;
        return total / count;
    }

    public Iterator<Student> iterator() {
        return students.iterator();
    }

    @Override
    public String toString() {
        return "Session{" +
                "course=" + course +
                ", students=" + students +
                ", startDate=" + startDate +
                ", numberOfCredits=" + numberOfCredits +
                ", url=" + url +
                '}';
    }
}
